$(() => {
  //window.alert('rectangle 1')
  //get dom elem
  let $width = $("#width"),
      $height = $("#height"),
      $btnCal = $("#calc"),
      $perimeter = $("#perimeter"),
      $area = $("#area"),
      $heighterr = $("#height-err"),
      $widtherr = $("#width-err");
  
  //字段校验
  //光标信息
  $width.focusout(() => {
    if(!validate($width, $widtherr)) {
       //光标还回来
       $width.select();
    }
  });
  $height.focusout(() => {
    if(!validate($height, $heighterr)) {
      $height.select();
    }
  });



  //calc button click
  //集中检验
  $btnCal.click(() => {

     //get value
     let w = Number($width.val()),
        h = Number($height.val());

    //集中检验
    if(validate($width, $widtherr) && validate($height, $heighterr)) {
    
      //calc
      //假设保留两位小数
      let p = Math.round((w+h)*2*Math.pow(10,2))/Math.pow(10,2),
          a = Math.round((w*h)*Math.pow(10,2))/Math.pow(10,2);
      //output
      $perimeter.val(p);
      $area.val(a);
  
    }
  });

  $forkMeGH.show("https://gitee.com/caimengting123");
});


function validate(input, output) {
  //is empty
  if(input.val() === "") {
    output.html("该字段不能为空！");
    return false;
  } else {
    output.html("");
  }

  //is number
  let val = Number(input.val());
  if(isNaN(val)) {
    output.html("该字段应该是数字！");
    return false;
  } else {
    output.html("");
  }

  //is +
  if(val < 0) {
    output.html("该数值不能小于0！");
    return false;
  } else {
    output.html("");
    return true;
  }
}
